<?php

	/**
	 * Script for uploading MBOX files to GoogleAppMail
	 * 
	 * This script uploads all the contents of a Thunderbird directory with
	 * e-mails to Google Email for App using Zend Frameworks MBox reading classes
	 * and Zend_Gdata classes.  
	 * 
	 * Reference: 
	 * http://code.google.com/apis/apps/email_migration/developers_guide_protocol.html
	 *
	 * Usage:
	 * 
	 * php UploadToGmail.php -account=user@example.com --password=pass --mailboxdir=/tmp/mbox   
 	 * 
	 * @author 		Martijn Dijksterhuis
	 * @copyright 	2009
	 * @license 	GPL v2.0
	 * @version	    $id$
	 */

	ini_set('memory_limit','128M');

    // Zend Framework
    // 
	// Initialize the application path and autoloading
	// To keep distributing the app simple we include the entire Zend framework

	define('APPLICATION_PATH', realpath(dirname(__FILE__)));
	set_include_path(implode(PATH_SEPARATOR, array(
	    APPLICATION_PATH . '/library',
	    get_include_path(),
	)));
	
	require_once 'Zend/Loader/Autoloader.php';
	Zend_Loader_Autoloader::getInstance();

	// Include our mailbox importer class 
	require_once 'classes/mboximporter.php';

	// Define some command line interface options
	$getopt = new Zend_Console_Getopt(array(
    'account|a=s'  => 'Google App Account (user@example.com)',
    'password|p=s' => 'Password',
	'mailboxdir|m=s' => 'Thunderbird mailbox directory',
	'upload|u' => 'Enable actual uploading of e-mail (disabled by default)',
	'batchlabel|b-s' => 'Label to tag uploaded messages with, set to empty for none. (default is \'test_upload\')',
	'readrestricted|r' => 'Also process Trash/Unsent Messages/Draft folders (disabled by default)',
    'help|h'     => 'Help -- usage message',
	));
	
	try 
	{
    	$getopt->parse();
	} 
	catch (Zend_Console_Getopt_Exception $e) 
	{
    	// Bad options passed: report usage
    	echo $e->getUsageMessage();
    	return false;
	}
	
	// If help requested, report usage message
	if ($getopt->getOption('h')) 
	{
    	echo $getopt->getUsageMessage();
    	return true;
	}
	
	// Initialize values based on presence or absence of CLI options
	$account      = $getopt->getOption('a');
	$password     = $getopt->getOption('p');
	$mailboxpath  = $getopt->getOption('m');
	$batchlabel   = $getopt->getOption('b');
	$restricted   = ($getopt->getOption('r') !== NULL);
	$upload		  = ($getopt->getOption('u') !== NULL);

	if ($account=="" || $password=="")
	{
		echo "Error: Need account & password" . PHP_EOL; 
		echo $getopt->getUsageMessage();
    	return false; 
	}
	
	if ($mailboxpath == "" || !is_dir($mailboxpath))
	{
		echo "Error: Need a valid Thunderbird mailbox directory" . PHP_EOL;
		echo $getopt->getUsageMessage();
    	return false; 
	}
	
	// The user specified an empty string, so we disable the batch label	
	if ($batchlabel === true)
		$batchlabel = '';

	// No batch label option was specified so we set the default		
	if ($batchlabel === NULL)
		$batchlabel = 'test_upload';

	
	// ----------------------------------------------------------------------------
	// Class Initialisation 
	// ----------------------------------------------------------------------------
	
	/* Setup the log class */
	$logger = new Zend_Log();
	$writer = new Zend_Log_Writer_Stream('php://output');
	$logger->addWriter($writer);

	/* Setup the already sent check class */
	$checksent = new AlreadySentCheck();
	$checksent->setLogger($logger);	
	$checksent->init();
			
	/* Setup the google uploader */
	$uploader = new BatchUploader();
	$uploader->setLogger($logger);
	$uploader->setAccount($account,$password);
	$uploader->setAlreadySentChecker($checksent);
	$uploader->setUploadLabel($batchlabel);
	$uploader->setUploadEnabled($upload);
			
	/* Setup the mailbox importer */
	$importer = new MBoxImporter();
	$importer->setLogger($logger);
	$importer->setAlreadySentChecker($checksent);
	$importer->setBatchUploader($uploader);
	$importer->setMailBoxPath($mailboxpath);	
	
	/* Start running */
	$importer->run();

